<style type="text/css">
    .hidden {
        display: none;
    }
</style>

<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1 class="m-0 text-dark">发送/编辑邮件</h1>
            </div><!-- /.col -->
            <div class="col-sm-6">
                <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item"><a th:href="@{admin}">首页</a></li>
                    <li class="breadcrumb-item active">我的邮箱</li>
                    <li class="breadcrumb-item active">发送/编辑邮件</li>
                </ol>
            </div><!-- /.col -->
        </div><!-- /.row -->
    </div><!-- /.container-fluid -->
</div>
<!-- Main content -->
<section class="content">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-3">
                <a href="#mailbox/normal" class="btn btn-primary btn-block mb-3">返回收件箱</a>

                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title">菜单</h3>

                        <div class="card-tools">
                            <button type="button" class="btn btn-tool" data-card-widget="collapse">
                                <i class="fas fa-minus"></i>
                            </button>
                        </div>
                    </div>
                    <div class="card-body p-0">
                        <ul class="nav nav-pills flex-column">
                            <li class="nav-item active">
                                <a href="#mailbox/normal" class="nav-link">
                                    <i class="fas fa-inbox"></i> <span>邮件列表</span>
<!--                                    <span class="badge bg-primary float-right">12</span>-->
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="#mailbox/send" class="nav-link">
                                    <i class="far fa-envelope"></i> 已发送
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="#" class="nav-link">
                                    <i class="far fa-file-alt"></i> 草稿
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="#mailbox/spam" class="nav-link">
                                    <i class="fas fa-filter"></i> 垃圾邮件
<!--                                    <span class="badge bg-warning float-right">65</span>-->
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="#" class="nav-link">
                                    <i class="far fa-trash-alt"></i> 回收站
                                </a>
                            </li>
                        </ul>
                    </div>
                    <!-- /.card-body -->
                </div>
                <!-- /.card -->
                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title">标签</h3>

                        <div class="card-tools">
                            <button type="button" class="btn btn-tool" data-card-widget="collapse">
                                <i class="fas fa-minus"></i>
                            </button>
                        </div>
                    </div>
                    <!-- /.card-header -->
                    <div class="card-body p-0">
                        <ul class="nav nav-pills flex-column">
                            <li class="nav-item">
                                <a class="nav-link" href="#"><i class="far fa-circle text-danger"></i> 重要邮件</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link" href="#"><i class="far fa-circle text-warning"></i> 垃圾邮件</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link" href="#"><i class="far fa-circle text-primary"></i> 普通邮件</a>
                            </li>
                        </ul>
                    </div>
                    <!-- /.card-body -->
                </div>
                <!-- /.card -->
            </div>
            <!-- /.col -->
            <div class="col-md-9">
                <div class="card card-primary card-outline">
                    <form id="sendEmail_form" enctype="multipart/form-data" method="post" action="#" onsubmit="return false;">
                        <div class="card-header">
                            <h3 class="card-title">邮件编写</h3>
                        </div>
                        <!-- /.card-header -->
                        <div class="card-body">
                            <div class="form-group">
                                <input name="address" class="form-control" placeholder="发送给:">
                            </div>
                            <div class="form-group">
                                <input name="title" class="form-control" placeholder="主题:">
                            </div>
                            <div class="form-group">
                            <div class="form-group">
                                <textarea id="articleField"></textarea>
                            </div>
                            </div>
                            <div class="form-group">
                                <div class="btn btn-default btn-file">
                                    <i class="fas fa-paperclip"></i> 上传附件
                                    <input type="file" id="emailFile">
                                </div>
                                <p class="help-block mt-1">文件大小限制. 32MB</p>
                            </div>
                        </div>
                    </form>
                    <!-- /.card-body -->
                    <div class="card-footer">
                        <div class="float-right">
                            <button type="button" class="btn btn-default"><i class="fas fa-pencil-alt"></i> 草稿</button>
                            <button data-toggle="modal" data-target="#sendEmailResult" onclick="sendEmail();" class="btn btn-primary">
                                <i class="far fa-envelope"></i> 发送
                            </button>
                        </div>
                        <button type="reset" class="btn btn-default"><i class="fas fa-times"></i> 取消</button>
                    </div>
                    <!-- /.card-footer -->
                </div>
                <!-- /.card -->
            </div>
            <!-- /.col -->
        </div>
        <!-- /.row -->
    </div><!-- /.container-fluid -->
</section>
<!-- /.content -->
<!-- 弹窗 -->
<div class="modal fade" id="sendEmailResult" tabindex="-1" role="dialog" aria-labelledby="sendEmailLabel"
     aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="sendEmailLabel">消息：</h5>
                <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body">发送成功！</div>
            <div class="modal-footer">
                <a class="btn btn-primary" data-dismiss="modal" aria-label="Close" href="#mailbox/normal">确定</a>
            </div>
        </div>
    </div>
</div>
<script th:src="@{js/jquery.min.js}"></script>
<script th:src="@{admin/plugins/ckeditor4/ckeditor.js}"></script>
<script>
    if (CKEDITOR.env.ie && CKEDITOR.env.version < 9)
        CKEDITOR.tools.enableHtml5Elements(document);

    CKEDITOR.config.height = 300;
    CKEDITOR.config.width = 'auto';
    CKEDITOR.replace('articleField')
</script>
<script>

    function sendEmail() {
        let form = $('#sendEmail_form');
        let formData = new FormData(form[0]);
        let emailContent = CKEDITOR.instances.articleField.document.getBody().getHtml();
        // console.log(emailContent);
        formData.append('content', emailContent);
        formData.delete(",");
        $.ajax({
            url: "/article/send",
            type: "post",   //上传文件需要使用post
            dataType: "json",
            data: formData,
            async: false,
            cache: false,
            contentType: false, //以及关闭这两个参数
            processData: false,
            success: function (result) {
                console.log(result)
                if(result){
                    layer.msg("提交成功");
                    $(window).attr('location','#mailbox/normal');
                }
            },
            error: function(request) {
                layer.msg("连接失败");
            }
        })
    }


    $(function () {
        initSliderUploadContent();


    })

    function initSliderUploadContent() {
        var showSlider = $("input[name=slider]:checked").val();
        if (parseInt(showSlider)) {
            $("#sliderImgContent").show();
        } else {
            $("#sliderImgContent").hide();
            $("#upload-slider-content").hide();
        }
    }

    function addOrUpdateArticle(status, addFlag) {
        if (validArticle()) {
            Core.confirm("确认保存文章？", function () {
                $("#status").val(status);
                if (addFlag) {
                    Core.postAjax("/article/add", $("#articleForm").serialize(), function (data) {
                        if (data.status === 200) {
                            CKEDITOR.instances.articleField.setData('');
                            Core.load("#content", "article/add");
                        }
                        layer.msg(data.msg)
                    })
                } else {
                    Core.postAjax("/article/edit", $("#articleForm").serialize(), function (data) {
                        layer.msg(data.msg)
                    })
                }
            })
        }
    }

    function validArticle() {
        var content = CKEDITOR.instances.articleField.getData();
        if (content) {
            $("#articleContentMd").val(content);
            $("#articleContent").val(content);
            // 文章标签必填项校验
            var tags = [];
            $('input[name="tag"]:checked').each(function () {
                tags.push($(this).val());
            });
            if (tags.length > 0) {
                return true;
            } else {
                layer.msg("请选择文章标签！")
                return false;
            }
        } else {
            layer.msg("请输入文章内容！")
            return false;
        }
    }

    $(function () {
        /*上传图片*/
        $("#upload-img-btn").click(function () {
            $('#upload-cover-content').toggle()
            $(".coverUploader").upload({
                server: '[[@{/attachment/upload}]]',
                swf: '/admin/img/Uploader.swf',
                imgAccept: true
            }, function (url, picker) {
                $("#coverImage").val(url);
                echoGtUploadResMethd(url, picker);
            });
        })

        $("#upload-slider-btn").click(function () {
            $('#upload-slider-content').toggle()
            $(".sliderUploader").upload({
                server: '[[@{/attachment/upload}]]',
                swf: '/admin/img/Uploader.swf',
                imgAccept: true
            }, function (url, picker) {
                $("#sliderImg").val(url);
                echoGtUploadResMethd(url, picker);
            });
        })
        $('input[type=radio][name=slider]').change(function () {
            initSliderUploadContent()
        });

    })
</script>
